# ---------------------------
#   Preprocess for analysis
#   Date: 20240404
#----------------------------

library(data.table)

# Import data
sas_file <- "./Data/analysisforprev.sas7bdat"
df <- haven::read_sas(sas_file)
df <- as.data.table(df)
df <- df[!is.na(odse) & !is.na(osse), ]

# Keep needed variable
df <- df[, .(
  school_name, school_type, area_name, name, card_no, nation, oducva,
  osucva, odds, oddc, gender, glass, graden, birth, checkdate, wave,
  age, odse, osse, myopia, highmyopia, odstdse, osstdse, basemyopia,
  basegrade, okwear, outcome, stdse, diffse, semster
)]

# Order for right analysis
df <- df[order(card_no, wave), ]
# make semster;
# grade : wave : semster
# 1:1:2
# 1:2:1
# 2:1:4
# 2:2:3
# semster = 2 * graden -(wave%%2 == 0 ) # nolint
# df[, myopia := myopia + (glass > 1)]
df <- df[, semster := 2 * graden - (wave %% 2 == 0)]
head(df[graden == 1 & wave == 1, ])
save(df, file = "./Data/rawdf.Rdata")

# Select first wave
load(file = "./Data/rawdf.Rdata")

df[, baseW := first(wave), by = .(card_no)]
df <- df[baseW == 1, ]

# Select not myopia
df <- df[, stdse := pmin(odstdse, osstdse, na.rm = TRUE)][!is.na(stdse), ]
df <- df[, baseSE := first(stdse), by = .(card_no)]
df <- df[baseSE >= -0.5 & !is.na(baseSE), ]

# Make outcome & time
df[, temp := cumsum(myopia), by = card_no]
df <- df[temp == myopia, ]
df[, temp := NULL]
df[, `:=`(
  event = max(myopia),
  time = last(semster)
), by = card_no]

# Calc person-year & sex facotrized
df[, diffDate := (last(checkdate) - first(checkdate)) / 365.25, by = card_no]
df <- df[, .SD[1], by = card_no]
df <- df[, .(card_no, name, birth, school_name, school_type, area_name, odse,
             osse, semster, baseSE, oddc, gender, event, time, checkdate,
             diffDate)]

df[, sex := factor(gender, levels = c(1, 2),
                   labels = c("Boys", "Girls"))]

# Keep semster<=18 (1-9 grade)
df <- df[semster <= 18, rawSE := pmin(odse, osse, na.rm = TRUE)]
df <- df[, diffDate := as.numeric(diffDate)]
df <- df[diffDate > 0, ]
df[,section:=fcase(
    area_name%in%c("和平区",'南开区','河西区','河东区','河北区','红桥区'),1,
    !is.na(area_name),2
)]
df[,section:=factor(section,levels=c(1,2),labels=c("Six central districts","Other"))]
save(df, file = "./Data/analysis.Rdata")
